package net.siufung.simple.demo.service;

import com.google.common.eventbus.AsyncEventBus;
import com.google.common.eventbus.SubscriberExceptionContext;
import com.google.common.eventbus.SubscriberExceptionHandler;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import net.siufung.core.event.AbstractEventBus;
import org.jetbrains.annotations.NotNull;
import org.springframework.stereotype.Component;

/**
 * @author 陈建峰
 * @date 2022/9/27 9:49 上午
 * @since V1.0.0
 */
@Component
@Slf4j
@AllArgsConstructor
public class DemoEventBus extends AbstractEventBus implements SubscriberExceptionHandler {

    private final AsyncEventBus asyncEventBus;

    @Override
    public void handleException(Throwable throwable, @NotNull SubscriberExceptionContext subscriberExceptionContext) {
        log.error("demo event handler exception, {}", throwable.getMessage());
    }

    @Override
    public void post(Object event) {
        asyncEventBus.post(event);
    }

    @Override
    public void addConsumer(Object obj) {
        asyncEventBus.register(obj);
    }

    @Override
    public void removeConsumer(Object obj) {
        asyncEventBus.unregister(obj);
    }
}
